package com.jindu.kernel.page;

import com.jindu.kernel.result.JsonResult;
import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.*;
import java.util.function.Consumer;

/**
 * @author jarvis
 * 2018-3-15
 */
public class AlphaPage<T> implements Iterable<T>, Serializable {


    private List<T> content = new ArrayList<>();
    private long totalElements;
    private int pageNo;
    private int PageSize;
    private int totalPages;

    private boolean hasPrevious;
    private boolean hasNext;

    public AlphaPage() {
    }

    public AlphaPage(Page page) {
        this.content = page.getContent();
        this.totalElements = page.getTotalElements();
        this.pageNo = page.getNumber();
        this.PageSize = page.getSize();
        this.totalPages=page.getTotalPages();
        this.hasPrevious=page.hasPrevious();
        this.hasNext=page.hasNext();
    }

    public AlphaPage(List<T> content, long totalElements, int pageNo, int PageSize) {
        this.content = content;
        this.totalElements = totalElements;
        this.pageNo = pageNo;
        this.PageSize = PageSize;
        this.hasPrevious=getPageNo() > 0;
        this.hasNext= getPageNo() + 1 < getTotalElements();
    }

    public JsonResult toJsonResult(){
        return JsonResult.getSuccessResult(this);
    }

    public boolean isHasPrevious() {
        return hasPrevious;
    }

    public void setHasPrevious(boolean hasPrevious) {
        this.hasPrevious = hasPrevious;
    }

    public boolean isHasNext() {
        return hasNext;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    public List<T> getContent() {
        return Collections.unmodifiableList(content);
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public long getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(long totalElements) {
        this.totalElements = totalElements;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return PageSize;
    }

    public void setPageSize(int pageSize) {
        PageSize = pageSize;
    }

    @Override
    public Iterator<T> iterator() {
        return  getContent().iterator();
    }

    @Override
    public String toString() {
        String contentType = "UNKNOWN";
        List<T> content = this.getContent();
        if (content.size() > 0) {
            contentType = content.get(0).getClass().getName();
        }

        return String.format("Page %s of %d containing %s instances", this.getPageNo() + 1, this.getTotalElements(), contentType);
    }
    @Override
    public void forEach(Consumer<? super T> action) {

    }

    @Override
    public Spliterator<T> spliterator() {
        return null;
    }


}
